/*
 * Copyright (c) YouYuan 2018. https://gitee.com/you-yuan
 */

package com.yuan.util.data;

import java.util.ArrayList;

/**
 * 分布式唯一自增ID生成器
 * 本类使用单例模式
 * @author YouYuan
 * @version 1.0
 * @CreateDate 2018/5/8 15:13
 * @UpdateDate 2018/5/8 15:13
 */
public class IdFactory {

    private IdFactory(){}

    private static class IdSequenceHolder {
        private static final IdSequence ID = new IdSequence(1, 1);
    }

    /**
     * 获取一个唯一的自增id
     * @return
     */
    public static long nextId(){
        return IdSequenceHolder.ID.nextId();
    }

    /**
     * 批量获取自增id
     * @param batchNum 批量数
     * @return
     */
    public static ArrayList<Long> nextIds(int batchNum){
        ArrayList<Long> ids = new ArrayList<Long>(batchNum);
        for (int i = 0; i < batchNum; i++) {
            ids.add(IdSequenceHolder.ID.nextId());
        }
        return ids;
    }
}
